package com.wd.live.view.fragment

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.blankj.utilcode.util.ToastUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.wd.live.databinding.FragmentVicinityBinding
import com.wd.live.model.base.BaseFragment
import com.wd.live.view.adapter.LiveRecyAdapter
import com.wd.live.viewmodel.ListViewModel


// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 作者：马海钊
 * 时间：2023年8月18日16:00:52
 * 功能：附近
 */
class FVicinityFragment : BaseFragment<ListViewModel, FragmentVicinityBinding>() {

    private var param1: String? = null
    private var param2: String? = null
    private var adapter: LiveRecyAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }


    override fun initData() {
        context?.let {
            XXPermissions.with(it)
                // 申请单个权限
                .permission(Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION)
                // 申请多个权限
    //            .permission(Permission.Group.CALENDAR)
                // 设置权限请求拦截器（局部设置）
                //.interceptor(new PermissionInterceptor())
                // 设置不触发错误检测机制（局部设置）
                //.unchecked()
                .request(object : OnPermissionCallback {

                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (!allGranted) {
                            ToastUtils.showLong("获取部分权限成功，但部分权限未正常授予")
                            return
                        }

                        ToastUtils.showLong("获取位置权限成功")
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            ToastUtils.showLong("被永久拒绝授权，请手动授予录音和日历权限")
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            context?.let { XXPermissions.startPermissionActivity(it, permissions) }
                        } else {
                            ToastUtils.showLong("获取录音和日历权限失败")
                        }
                    }
                })
        }

        binding.weizhiStart.setOnClickListener {
            initon()
        }
        //直播列表
        initList()


        //动画


    }
    private fun initList(){
        viewModel.getLive(100, 1, 10)
        binding.lookAll.setOnCheckedChangeListener { button, b ->
            if (b){
                viewModel.getLive(100, 1, 10)
            }
        }
        binding.lookNv.setOnCheckedChangeListener { button, b ->
            if (b){
                viewModel.getLive(102, 1, 10)
            }
        }
        binding.lookMan.setOnCheckedChangeListener { button, b ->
            if (b){
                viewModel.getLive(103, 1, 10)
            }
        }
        viewModel.liveMutableLiveData.observe(this, Observer {
            //Toast.makeText(context,it.result[0].nickName,Toast.LENGTH_SHORT).show()
            adapter = context?.let { it1 -> LiveRecyAdapter(it1, it.result) }
            binding.fujinRecyId.layoutManager = LinearLayoutManager(context)
            binding.fujinRecyId.adapter = adapter
        })
    }
    override fun getViewBinding() = FragmentVicinityBinding.inflate(layoutInflater)

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment VicinityFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance() =
            FVicinityFragment().apply {

            }
    }

    private fun initon() {
        //声明AMapLocationClient类对象
        //声明AMapLocationClient类对象
        var mLocationClient: AMapLocationClient? = null
        //声明定位回调监听器
        //声明定位回调监听器
        val mLocationListener = AMapLocationListener {
            ToastUtils.showLong("${it.longitude}")
            Log.i("aaa", "initon" + it.latitude)
            if (it.latitude == 0.0){

                binding.wz.setText("未设置位置")
                binding.weizhiColse.visibility=View.VISIBLE
                binding.fujinRecyId.visibility= View.GONE
                binding.donghuaId.visibility=View.GONE

            }else{

                binding.fujinRecyId.visibility=View.VISIBLE
                binding.donghuaId.visibility=View.VISIBLE
                binding.weizhiColse.visibility=View.GONE
                binding.weizhiStart.visibility=View.GONE
                binding.wz.setText("${it.street}")
            }
        }
        //初始化定位
        //初始化定位
        mLocationClient = AMapLocationClient(context)
        //设置定位回调监听
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener)


        //声明AMapLocationClientOption对象
        //声明AMapLocationClientOption对象
        var mLocationOption: AMapLocationClientOption? = null
        //初始化AMapLocationClientOption对象
        //初始化AMapLocationClientOption对象
        mLocationOption = AMapLocationClientOption()

        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(true);

        //获取最近3s内精度最高的一次定位结果：
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption.setOnceLocationLatest(true);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption.setInterval(1000);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(true);
        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        mLocationOption.setHttpTimeOut(20000);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }
}
